import { Message } from 'element-ui';


// 下载文件链接
export function downloadUrl(res, fileName) {
    fetch(res).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
        const aLink = document.createElement('a')
        aLink.href = URL.createObjectURL(blob)
        aLink.download = fileName;
        document.body.appendChild(aLink)
        aLink.click()
        aLink.remove();
    })
}

// 下载文件流
export function requestDown(res, fileName) {
    // 对下载的流进行处理，直接从浏览器下载下来
    const content = res;
    const blob = new Blob([content], {
        // 下载的文件格式自己在这边更改type的值就好了
        type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    });
    if ("download" in document.createElement("a")) {
        const elink = document.createElement("a");
        elink.download = fileName;
        elink.style.display = "none";
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href);
        document.body.removeChild(elink);

        Message.success("下载成功，请查看")
    } else {
        navigator.msSaveBlob(blob, fileName);
    }
}
